package com.bangdream.michelia.presenter;

import android.app.Dialog;
import android.util.Log;

import com.bangdream.michelia.contract.LiveContract;
import com.bangdream.michelia.entity.BaseEntity;
import com.bangdream.michelia.entity.LiveBean;
import com.bangdream.michelia.entity.LiveUrlBean;
import com.bangdream.michelia.entity.ReplayBean;
import com.bangdream.michelia.model.ILiveModelImpl;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.rejava.BaseObserverEntityNormalHttp;
import com.bangdream.michelia.tool.rejava.BaseObserverStringNormalHttp;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;
import com.bangdream.michelia.utils.L;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @date on 2018/8/1 23:38
 * @email 909732184@qq.com
 */
public class LivePersenter<V> extends BasePresenter<V> {
    private LiveContract.ILiveModel model;

    public LivePersenter() {
        this.model = new ILiveModelImpl();
    }
    public void getLiveListData(final int pageNo, int pageSize, Map paramsMap, Dialog dialog){
        final LiveContract.ILiveView view = getMyView();
        if(view==null)
            return;
        if(paramsMap==null){
            paramsMap = new HashMap();
        }
        setPd(dialog);
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
        paramsMap.put("delFlag","0");
        paramsMap.put("state","1");

        Map finalParamsMap = paramsMap;
        TokenManagerDef.work(() -> model.getLiveList(finalParamsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<LiveBean>>(this,pd) {
                    @Override
                    public void onHandleSuccess(List<LiveBean> liveBeans) {
                        if(getMyView()==null)
                            return;
                        view.setLiveList(true,pageNo,liveBeans,getDescribe(liveBeans));
                    }
                });
    }
    public void getLiveDetails(String id, Dialog dialog){
        final LiveContract.ILiveView view = getMyView();
        if(view==null)
            return;
        setPd(dialog);
        TokenManagerDef.work(() -> model.getLiveDetails(id)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<LiveBean>(this,pd) {
                    @Override
                    public void onHandleSuccess(LiveBean liveBeans) {
                        if(view==null)
                            return;
                        view.setLiveDetails(true,liveBeans,getDescribe(liveBeans));
                    }
                });
    }
    private LiveContract.ILiveView getMyView(){
        return (LiveContract.ILiveView) obtainView();
    }

    public void getLiveUrl(String token,String appName, String id) {
        final LiveContract.ILiveView view = getMyView();
        if(view==null)
            return;
        TokenManagerDef.work(() -> model.getLiveUrl(token,appName,id)).compose(TokenManagerDef.newInstance().getTransformer())
//        model.getLiveUrl(token,appName,id)
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<String>>(this,pd) {
                    @Override
                    public void onHandleSuccess(List<String> liveBeans) {
                        view.setLiveUrl(true,liveBeans);

                    }
                });
//                .subscribe(new Observer<BaseEntity<List<String>>>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(LiveUrlBean o) {
//                        view.setLiveUrl(true,o);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//                    }
//
//                });
    }
    public void getReplayLiveUrl(String token, String id) {
        final LiveContract.ILiveView view = getMyView();
        if(view==null)
            return;
        TokenManagerDef.work(() -> model.getReplayLiveUrl(token,id)).compose(TokenManagerDef.newInstance().getTransformer())
//        model.getLiveUrl(token,appName,id)
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<ReplayBean>>(this,pd) {
                    @Override
                    public void onHandleSuccess(List<ReplayBean> liveBeans) {

                        List<String> list=new ArrayList<>();
                        if(!liveBeans.isEmpty()){
                            for(ReplayBean bean:liveBeans){
                                list.add(bean.getReviewUrl());
                            }
                        }
                        view.setRePlayLiveUrl(true,list);
                    }
                });
    }
}
